﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MSAcademic.v2.Infrastructure.Article;
using MSAcademic.v2.Infrastructure.ContentSection;
using MSAcademic.v2.Infrastructure.SubSection;
using MSAcademic.v2.Infrastructure.User;

namespace MSAcademic.v2.Web.Models.Home
{
    public partial class ContentSectionViewModel
    {
        public int ContentSectionId { get; set; }
    }
    public partial class ContentSectionViewModel
    {
        public List<ISubSection> ListOfSubSectionsForThisContentSection
        {
            get
            {
                if (_subSectionsForContentSection == null)
                {
                    _subSectionsForContentSection = new Lazy<List<ISubSection>>(() => _subSectionQueries.GetAllForContentSection(ContentSectionId).ToList());
                }
                return _subSectionsForContentSection.Value;
            }
        }
        private Lazy<List<ISubSection>> _subSectionsForContentSection;

        public List<Tuple<ISubSection, List<Tuple<IArticle,IUser>>>> SubSectionsWithTheirArticles
        {
            get
            {
                if (_subSectionsWithTheirArticles == null)
                {
                    _subSectionsWithTheirArticles = new Lazy<List<Tuple<ISubSection, List<Tuple<IArticle,IUser>>>>>(() => 
                        (from data in _contentSectionQueries.GetDataForContentSection(ContentSectionId,false)
                         select new Tuple<ISubSection, List<Tuple<IArticle,IUser>>>(data.Item1,
                             (from a in data.Item2
                             let author=_userQueries.GetArticleAuthor(a.Id)
                             select  new Tuple<IArticle,IUser>(a,author) ).ToList())).ToList());
                }
                return _subSectionsWithTheirArticles.Value;
            }
        }
        private Lazy<List<Tuple<ISubSection, List<Tuple<IArticle,IUser>>>>> _subSectionsWithTheirArticles;
    }
    public partial class ContentSectionViewModel
    {
        private readonly IContenSectionQueries _contentSectionQueries;
        private readonly ISubSectionQueries _subSectionQueries;
        private readonly IUserQueries _userQueries;
        public ContentSectionViewModel(ISubSectionQueries subSectionQueries,
            IContenSectionQueries contentSectionQueries,
            IUserQueries userQueries)
        {
            
            _subSectionQueries = subSectionQueries;
            _contentSectionQueries = contentSectionQueries;
            _userQueries = userQueries;
        }
    }
}